package com.cmhi.softmbh.receiver;

import android.app.DevInfoManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.chinamobile.SWDevInfoManager;
import com.cmhi.softmbh.MyApplication;
import com.softmbh.kellyplatform.util.AppUtils;

public class CDN_Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        AppUtils.debugLog("CDN_Receiver onReceive() action : " + intent.getAction());
        SWDevInfoManager.getInstance(MyApplication.myApplication).getValue(DevInfoManager.CDN_TYPE, cdnType -> {//华为：HW，中兴：ZTE
            try {
                AppUtils.debugLog("updateZWCND-MyApplication " + cdnType + "  start");
                if (cdnType.equals("HW")) {
                    setZwData(DevInfoManager.EPG_ADDRESS, "http://223.110.245.22:33200/EPG");
                    setZwData(DevInfoManager.CDN_ADDRESS, "http://183.207.248.71:80");
                    setZwData(DevInfoManager.CDN_ADDRESS_BACK, "http://183.207.249.71:80");
                } else {
                    //软终端默认调用中兴 CDN，卓望软终端鉴权默认数据为中兴 CDN
                    setZwData(DevInfoManager.EPG_ADDRESS, "http://223.110.239.37:9330/EPG");
                    setZwData(DevInfoManager.CDN_ADDRESS, "http://223.110.239.40:6060");
                    setZwData(DevInfoManager.CDN_ADDRESS_BACK, "http://223.110.240.40:6060");
                }
                AppUtils.debugLog("updateZWCND-MyApplication " + cdnType + "  finish");
            } catch (Exception e) {
                AppUtils.debugLog("updateZWCND-MyApplication " + cdnType + "  error : " + e);
                e.printStackTrace();
            }
        });
    }

    //结束进程
    private void exit() {
        //调用系统 API 结束进程
        android.os.Process.killProcess(android.os.Process.myPid());
        //结束整个虚拟机进程，注意如果在 manifest 里用 android:process 给 APP 指定了不止一个进程，则只会结束当前进程
        System.exit(0);
    }
    //写入卓望数据
    private void setZwData(String key, String value) {
        SWDevInfoManager.getInstance(MyApplication.myApplication)
                .update(key,
                        value,
                        DevInfoManager.Default_Attribute,
                        result -> AppUtils.debugLog(key + " : " + value + " : " + (result == 0 ? "success" : result)));
    }
}
